package com.agriculture.competition.service.impl;

import com.agriculture.common.core.utils.DateUtils;
import com.agriculture.common.security.service.TokenService;
import com.agriculture.competition.domain.JaCompetitionInvitation;
import com.agriculture.competition.domain.vo.JaCompetitionInvitationVo;
import com.agriculture.competition.mapper.JaCompetitionInvitationMapper;
import com.agriculture.competition.mapper.JaGroupUserMapper;
import com.agriculture.competition.service.IJaCompetitionInvitationService;
import com.agriculture.system.api.model.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 比赛受邀人Service业务层处理
 *
 * @author agriculture
 * @date 2025-03-10
 */
@Service
@Slf4j
public class JaCompetitionInvitationServiceImpl implements IJaCompetitionInvitationService {
    @Autowired
    private JaCompetitionInvitationMapper jaCompetitionInvitationMapper;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private JaGroupUserMapper jaGroupUserMapper;

    /**
     * 查询比赛受邀人
     *
     * @param id 比赛受邀人主键
     * @return 比赛受邀人
     */
    @Override
    public JaCompetitionInvitation selectJaCompetitionInvitationById(Long id) {
        return jaCompetitionInvitationMapper.selectJaCompetitionInvitationById(id);
    }

    /**
     * 查询比赛受邀人列表
     *
     * @param jaCompetitionInvitation 比赛受邀人
     * @return 比赛受邀人
     */
    @Override
    public List<JaCompetitionInvitation> selectJaCompetitionInvitationList(JaCompetitionInvitationVo jaCompetitionInvitation) {
        LoginUser loginUser = tokenService.getLoginUser();
        jaCompetitionInvitation.setInvitedPerson(loginUser.getUserid());
        return jaCompetitionInvitationMapper.selectJaCompetitionInvitationList(jaCompetitionInvitation);
    }

    /**
     * 新增比赛受邀人
     *
     * @param jaCompetitionInvitation 比赛受邀人
     * @return 结果
     */
    @Override
    public int insertJaCompetitionInvitation(JaCompetitionInvitation jaCompetitionInvitation) {
        int i = 0;
        JaCompetitionInvitation jaCompetitionInvitation1 = jaCompetitionInvitationMapper.queryUserInvitedPerson(jaCompetitionInvitation);
        if (jaCompetitionInvitation1 != null) {
            JaCompetitionInvitation ss = new JaCompetitionInvitation();
            ss.setId(jaCompetitionInvitation1.getId());
            BeanUtils.copyProperties(jaCompetitionInvitation, ss);
            i = jaCompetitionInvitationMapper.updateJaCompetitionInvitation(ss);
        } else {
            jaCompetitionInvitation.setCreateTime(DateUtils.getNowDate());
            i = jaCompetitionInvitationMapper.insertJaCompetitionInvitation(jaCompetitionInvitation);
        }
        return i;
    }

    /**
     * 修改比赛受邀人
     *
     * @param jaCompetitionInvitation 比赛受邀人
     * @return 结果
     */
    @Override
    public int updateJaCompetitionInvitation(JaCompetitionInvitation jaCompetitionInvitation) {
        jaCompetitionInvitation.setUpdateTime(DateUtils.getNowDate());
//        //修改受邀人状态
//        JaGroupUser jaGroupUser = new JaGroupUser();
//        jaGroupUser.setUserId(jaCompetitionInvitation.getInvitedPerson());
//        jaGroupUser.setStatus(jaCompetitionInvitation.getStatus());
//        jaGroupUser.setUpdateTime(new Date());
//        jaGroupUserMapper.updateJaGroupUserStatus(jaGroupUser);
        return jaCompetitionInvitationMapper.updateJaCompetitionInvitation(jaCompetitionInvitation);
    }

    /**
     * 批量删除比赛受邀人
     *
     * @param ids 需要删除的比赛受邀人主键
     * @return 结果
     */
    @Override
    public int deleteJaCompetitionInvitationByIds(Long[] ids) {
        return jaCompetitionInvitationMapper.deleteJaCompetitionInvitationByIds(ids);
    }

    @Override
    public int deleteJaCompetitionInvitationByUserIds(Map<String, Object> vo) {
        log.info("删除邀请人：" + vo);
        int i = 0;
        try {
            LoginUser loginUser = tokenService.getLoginUser();
            vo.put("userId", loginUser.getUserid());
            log.info("删除邀请人-loginUser.getUserid()：" + loginUser.getUserid());
            i = jaCompetitionInvitationMapper.deleteJaCompetitionInvitationByUserIds(vo);
        } catch (Exception e) {
            log.error("删除邀请人失败", e);
        }
        return i;
    }

    /**
     * 删除比赛受邀人信息
     *
     * @param id 比赛受邀人主键
     * @return 结果
     */
    @Override
    public int deleteJaCompetitionInvitationById(Long id) {
        return jaCompetitionInvitationMapper.deleteJaCompetitionInvitationById(id);
    }
}
